package healththink

import com.the6hours.grails.springsecurity.facebook.FacebookAuthToken
import org.springframework.security.core.authority.GrantedAuthorityImpl
import org.springframework.security.core.GrantedAuthority

class FacebookAuthService 
{
    void onCreate(FacebookUser user, FacebookAuthToken token) {
        log.info("Creating user: $user for fb user: $token.uid")
    }

    void afterCreate(FacebookUser user, FacebookAuthToken token) {
        log.info("User created: $user for fb user: $token.uid")
    }

    FacebookUser findUser(Long uid) 
    {
        log.info("Search for facebook user with id $uid")
        return FacebookUser.findByUid(uid)
    }

    FacebookUser create(FacebookAuthToken token) {
        log.info("Create domain for facebook user $token.uid")
        User userContributor = new User(
                username: "$token.uid",
                firstName: "$token.name",
                password: '********',
                enabled: true,
                accountExpired:  false,
                accountLocked: false,
                passwordExpired: false
        )
        userContributor.save()
        
        def contributorRole = SecRole.findByAuthority(Contributor) ?: 
                    new SecRole(authority: Contributor).save()
        def fbRole = SecRole.findByAuthority('ROLE_CONTRIBUTOR') ?: 
                    new SecRole(authority: 'ROLE_CONTRIBUTOR').save()
                    
        SecUserSecRole.create userContributor, contributorRole
        SecUserSecRole.create userContributor, fbRole
        
        FacebookUser fbUser = new FacebookUser(
                uid: token.uid,
                accessToken: token.accessToken,
                user: userContributor
        )
        
        fbUser.save()
        return fbUser
    }
    
    User createAppUser(FacebookUser user, FacebookAuthToken token) 
    {
        log.info("Create app user for facebook user $token.uid")
        User userContributor = new User(
                username: "$token.uid",
                firstName: "$token.name",
                password: '********',
                enabled: true,
                accountExpired:  false,
                accountLocked: false,
                passwordExpired: false
        )
        userContributor.save(failOnError: true)
        return userContributor
    }
    
    void createRoles(FacebookUser user) 
    {
        log.info("Create role for facebook user $user.uid")
        
        User userContributor = User.findByUsername("$user.uid")
        
        if (userContributor != null)
        {
            def contributorRole = SecRole.findByAuthority(Contributor) ?: 
                        new SecRole(authority: Contributor).save()
            def fbRole = SecRole.findByAuthority('ROLE_CONTRIBUTOR') ?: 
                        new SecRole(authority: 'ROLE_CONTRIBUTOR').save()

            SecUserSecRole.create userContributor, contributorRole
            SecUserSecRole.create userContributor, fbRole
        }
    }
    
    def getPrincipal(FacebookUser user) 
    {
        log.info("Ger principal for facebook user #$user.id")
        return user.user
    }
    
    Collection<GrantedAuthority> getRoles(FacebookUser user) {
        log.info("Get roles for facebook user #$user.id")
        return user.user.authorities.collect {
            new GrantedAuthorityImpl(it.authority)
        }
    }
    
    void prepopulateAppUser(User user, FacebookAuthToken token) {
        log.info("Prepopulate app user")
        user.password = '*******'
        user.username = "$token.uid"
        user.firstName = "$token.name"
        user.accountExpired = false
        user.accountLocked = false
        user.enabled = true
        user.passwordExpired = false
    }
}
